package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constants.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(path = "/menu")
public class MenuController {

    @Reference
    private MenuService menuService;


    @RequestMapping(path = "/getMenu")
    public Result getMenu(){
        try{
            //获取用户名
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            List<Map<String,Object>> list = menuService.getMenu(user.getUsername());
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_MENU_FAIL);
        }

    }

    @RequestMapping(value = "/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        return menuService.findPage(queryPageBean);
    }
    @RequestMapping(value = "/findById")
    public Result findById(@RequestParam(value = "id") Integer id){
        Menu menu = menuService.findById(id);
        if(menu!=null){
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,menu);
        }
        return new Result(false, MessageConstant.QUERY_MENU_FAIL);
    }
    @RequestMapping(value = "/edit")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }
    @RequestMapping(value = "/deleteById")
    public Result deleteById(@RequestParam(value = "id") Integer id){
        try {
            menuService.delete(id);
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        } catch (RuntimeException e){
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }
}
